'use strict';

const Service = require('egg').Service;

class AuthService extends Service {
  async verifyToken(ctx) {
    const token = ctx.request.header.authorization;
    if (!token) {
      this.ctx.throw(401, 'Unauthorized: No token provided');
    }
    try {
      const decode = this.app.jwt.verify(token, this.app.config.jwt.secret);
      if (!decode) {
        this.ctx.throw(401, 'Unauthorized: Invalid token');
      }
      return decode.id;
    } catch (error) {
      this.ctx.throw(401, 'Unauthorized: Token verification failed');
    }
  }
}

module.exports = AuthService;
